package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.IhotArticleService;
import com.heima.behavior.dto.ArticleStreamMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author: itheima
 * @create: 2022-05-28 14:37
 */
@Slf4j
@Component
public class HotArticleResultListener {

    @Autowired
    private IhotArticleService ihotArticleService;


    /**
     *
     * @param record
     */
    @KafkaListener(topics = "hot_article_result_topic")
    public void handleMessage(ConsumerRecord<String, String> record) {
        if(StringUtils.isNotBlank(record.value())){
            log.info("实时计算热点文章行为监听器："+record.value());
            ArticleStreamMessage articleStreamMessage = JSON.parseObject(record.value(), ArticleStreamMessage.class);
            ihotArticleService.updateArticleCacheAndInfo(articleStreamMessage);

        }
    }


}
